package com.example.coreshopapp.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.example.coreshopapp.R;
import com.example.coreshopapp.bean.GoodsData;
import com.example.coreshopapp.bean.GoodsReq;
import com.example.coreshopapp.bean.OrderReq;

import java.util.ArrayList;
import java.util.List;

import bean.OrderItemBean;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
    private List<GoodsData> goodsData = new ArrayList<>();
    private List<OrderItemBean> orderItemList = new ArrayList<>();// 购物车数据

    public GoodsAdapter(List<GoodsData> goodsData, List<OrderItemBean> orderItemList) {
        this.goodsData = goodsData;
        this.orderItemList = orderItemList;
    }

    public void updateData(List<GoodsData> goodsData, List<OrderItemBean> orderItemList) {
        this.goodsData = goodsData;
        this.orderItemList = orderItemList;
        notifyDataSetChanged();  // 通知 RecyclerView 刷新
    }

    @NonNull
    @Override
    public GoodsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods, parent, false);
//        View view = View.inflate(parent.getContext(), R.layout.item_goods, null);
        return new ViewHolder(inflate);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull GoodsAdapter.ViewHolder holder, int position) {
        GoodsData item = goodsData.get(position);
        holder.goodsNameTv.setText(item.getName());
        holder.goodsDescTv.setText("/ " + item.getUnit());
        holder.cartNum.setText("");
        holder.minusBtn.setVisibility(View.GONE);

        // 有优惠价
        holder.discountBox.setVisibility(item.getPromotionPrice() > 0 ? View.VISIBLE : View.GONE);
        holder.costPriceLy.setVisibility(item.getPromotionPrice() > 0 ? View.VISIBLE : View.GONE);
        holder.goodsPriceTv.setText((item.getPromotionPrice() > 0 ? item.getPromotionPrice() : item.getPrice()) + "");
        holder.costPriceTv.setText(item.getPromotionPrice() > 0 ? item.getPrice() + "" : "");
        holder.itemView.setBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.goods_bg));

        if (orderItemList != null) {
            orderItemList.forEach(e -> {
                if (e.getId() == item.getId()) {
                    holder.minusBtn.setVisibility(View.VISIBLE);
                    holder.cartNum.setVisibility(View.VISIBLE);
                    holder.cartNum.setText(e.getNums() + "");
                    holder.itemView.setBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.white));
                }
            });

        }

        holder.addBtn.setOnClickListener(view -> {
            // 添加事件
            if (onItemAddClickListener != null) {
                onItemAddClickListener.onItemAddClick(item);
            }
        });
        holder.minusBtn.setOnClickListener(view -> {
            // 减少事件
            // 判断当前商品价格是否大于1，等下传递过来
            if (onItemMinusClickListener != null) {
                orderItemList.forEach(e -> {
                    if (item.getId() == e.getId()) {
                        onItemMinusClickListener.onItemMinusClick(item, e);
                    }
                });
            }
        });
        // 点击数量，弹出弹窗，输入数字
        holder.cartNum.setOnClickListener(view -> {
            if (onItemNumClickListener != null) {
                orderItemList.forEach(e -> {
                    if (item.getId() == e.getId()) {
                        onItemNumClickListener.onItemAddClick(item, e);
                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodsData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView goodsNameTv, goodsDescTv, goodsPriceTv, discountBox, checkedSpecTv, minusBtn, addBtn, cartNum, costPriceTv;
        private LinearLayout calculateLy, costPriceLy;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsNameTv = itemView.findViewById(R.id.goods_name_tv);
            goodsDescTv = itemView.findViewById(R.id.goods_desc_tv);
            goodsPriceTv = itemView.findViewById(R.id.goods_price_tv);
            discountBox = itemView.findViewById(R.id.discount_box);
            calculateLy = itemView.findViewById(R.id.calculate_ly);
            checkedSpecTv = itemView.findViewById(R.id.checked_spec_tv);
            minusBtn = itemView.findViewById(R.id.minus_btn);
            addBtn = itemView.findViewById(R.id.add_btn);
            cartNum = itemView.findViewById(R.id.cart_number);
            costPriceLy = itemView.findViewById(R.id.cost_price_ly);
            costPriceLy = itemView.findViewById(R.id.cost_price_ly);
            costPriceTv = itemView.findViewById(R.id.cost_price_tv);
        }
    }

    // 减数量事件
    private OnItemMinusClickListener onItemMinusClickListener;

    public interface OnItemMinusClickListener {
        void onItemMinusClick(GoodsData item, OrderItemBean orderItem);
    }

    public void setOnItemMinusClickListener(GoodsAdapter.OnItemMinusClickListener onMyItemMinusClickListener) {
        onItemMinusClickListener = onMyItemMinusClickListener;
    }


    private OnItemAddClickListener onItemAddClickListener;

    public interface OnItemAddClickListener {
        void onItemAddClick(GoodsData item);
    }

    public void setOnItemAddClickListener(GoodsAdapter.OnItemAddClickListener onMyItemAddClickListener) {
        onItemAddClickListener = onMyItemAddClickListener;
    }


    private OnItemNumClickListener onItemNumClickListener;

    public interface OnItemNumClickListener {
        void onItemAddClick(GoodsData item, OrderItemBean orderItem);
    }

    public void setOnItemNumClickListener(GoodsAdapter.OnItemNumClickListener onMyItemNumClickListener) {
        onItemNumClickListener = onMyItemNumClickListener;
    }

}
